package douaPahare.operator;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import douaPahare.container.Stare;


public class T12 extends JButton implements Operator {
	
    private JPanel panel;
	
	public T12(String nume,JPanel p) {
		super(nume);
		panel = p;
	}

	public void executa() throws Exception {
	    if(evaluare(Stare.getStareStatic()))
	    	Stare.setStareStatic(actiune(Stare.getStareStatic()));
		else throw new Exception("Nu se poate transfera din primul pahar in al doilea pahar!");
	}
	
	public boolean evaluare(Stare st) {
		if(st.getX() == 0 || st.getY() == 4) 
			return false;
		else return true;
	}
	
	public Stare actiune(Stare st) {
		final JProgressBar p1 = (JProgressBar)panel.getComponent(0);
		final JProgressBar p2 = (JProgressBar)panel.getComponent(2);
		if((st.getX() - (4 - st.getY())) <= 0) { // (ce ramane in paharul 1) se toarna tot paharul 1 in paharul 2
			p1.setValue(0);
			p2.setValue(st.getY() + st.getX());	
			return new Stare(0,st.getY() + st.getX());
		} else { 
			p1.setValue(st.getX() - (4 - st.getY()));
			p2.setValue(st.getY() + (4 - st.getY()));
			return new Stare(st.getX() - (4 - st.getY()),st.getY() + (4 - st.getY()));
		}
	}
	
	public Stare actiune2(Stare st) {
		if((st.getX() - (4 - st.getY())) <= 0) { // (ce ramane in paharul 1) se toarna tot paharul 1 in paharul 2
			return new Stare(0,st.getY() + st.getX());
		} else { 
			return new Stare(st.getX() - (4 - st.getY()),st.getY() + (4 - st.getY()));
		}
	}
	
}
